package com.zhku.zhny.admin.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.alibaba.fastjson.JSON;
import com.zhku.zhny.admin.constant.ConstantFilePath;
import com.zhku.zhny.admin.exception.RemoveException;
import com.zhku.zhny.admin.exception.SearchException;
import com.zhku.zhny.admin.service.AdminService;
import com.zhku.zhny.admin.service.AdminServiceImpl;
import com.zhku.zhny.domain.Group;
import com.zhku.zhny.domain.GroupPageBean;
import com.zhku.zhny.util.BaseServlet;
import com.zhku.zhny.util.FileOperationUtil;
import com.zhku.zhny.util.RandomIdFactory;

@WebServlet("/info/group")
public class GroupInfoServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;
	
	public String addGroup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			Map<String, String[]> map = null;
			try {
				map = FileOperationUtil.upload(request, ConstantFilePath.LOCALGROUPFILEPATH);
				Group group = new Group();
				map.put("groupId", new String[] {RandomIdFactory.getRandomId()});
				BeanUtils.populate(group, map);
				AdminService adminService = new AdminServiceImpl();
				int i = adminService.addGroupInfo(group);
				if (i > 0) {
					return "redirect:/admin/success.html";
				}else {
					return "redirect:/admin/fail.html";
				}
			} catch (Exception e) {
				e.printStackTrace();
				return "redirect:/admin/fail.html";
			}
	}
	
	/**
	 * 删除单个
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void removeGroup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String groupId = request.getParameter("groupId");
		AdminService adminService = new AdminServiceImpl();
		boolean result = false;
		try {
			result = adminService.removeGroupById(groupId);
			response.getWriter().print(result);
		} catch (RemoveException e) {
			e.printStackTrace();
			response.getWriter().print(result);
		}
	}
	
	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void removeGroups(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String param = request.getParameter("ids");
		String[] ids = param.split(",");
		AdminService adminService = new AdminServiceImpl();
		boolean result = false;
		try {
			result = adminService.removeGroupByIds(ids);
			if(result) {
				response.getWriter().print(result);
			}else {
				response.getWriter().print(result);
			}
		} catch (RemoveException e) {
			e.printStackTrace();
			response.getWriter().print(result);
		}
	}
	
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void modifyGroup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}
	
	/**
	 * 分页查询
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void fySearchGroups(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String groupName = request.getParameter("groupName");
		String currentPage = request.getParameter("currentPage");
		int pageSize = 10;// 设置每页显示记录数
		//调用业务逻辑
		AdminService adminService = new AdminServiceImpl();
		GroupPageBean gpb = new GroupPageBean();
		try {
			gpb = adminService.findGroupsByName(groupName,currentPage,pageSize);
			String data = JSON.toJSONString(gpb);// 将数据封装为json对象
			// 响应回客户端
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(data);
		} catch (SearchException e) {
			e.printStackTrace();
			response.getWriter().print("");
		}
	}
	
	/**
	 * 查询出所有小组信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void searchGroups(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminService adminService = new AdminServiceImpl();
		try {
			List<Group>groups = adminService.findAllGroups();
			Group group = null;
			for (int i = 0; i < groups.size(); i++) {
				group = groups.get(i);
				// 修改小组图片访问路径
				String newPath = ConstantFilePath.GROUPDIRECTORY+group.getGroupImgPath();
				group.setGroupImgPath(newPath);
			}
			String data = JSON.toJSONString(groups);
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(data);
		} catch (SearchException e) {
			e.printStackTrace();
			response.getWriter().print("");
		}
	}
}
